class buildingsMap {
	constructor(dom) {
		this.dom = dom;
		this.bdstr = 'a';
		this.base_info = 'buildings-room-utf8.csv';
		this.room_info = 'room-depart.csv';
		this.room_pg = 'room-pg.csv';
	}
	setBds(bdstr) {
		this.bdstr = bdstr;
		let dom = this.dom;
		let room_w = 70;
		let bds = bdstr.toUpperCase().split('|');
		let bds_width = 0;
		dom.innerHTML = '';
		bds.forEach(bd => {
			setBd(bd);
			bds_width += (buildings[bd].length * room_w) + 96;
		});
		dom.style.width = bds_width + 'px';

		function setBd(abc) {
			let bd_width = (buildings[abc].length * room_w) + 45 + 'px';
			let bd_title = `${buildings[abc].type}${abc}【${buildings[abc].name}】布局图`;
			bd_title = `<div id="${abc}-title" class="bdtitle" >${bd_title}</div>`;
			let bd_floor = `<div id="${abc}-floor" class="bd" style="width:${bd_width}"></div>`;
			let bd_div = `<div id="${abc}" class="bds">${bd_title+bd_floor}</div>`;
			dom.innerHTML += bd_div;
			setFloor(abc);
		}

		function setFloor(abc) { //
			let floor_info = buildings[abc].data;
			let bd_div = document.getElementById(abc + '-floor');
			let fns_class = ['floor-N', 'floor-S'];
			let room_title = '<div class="room-title"></div>';
			let room_cont = '<div class="room-cont"> </div>';
			let room_html;
			//==设置楼宇、楼层框架及基础信息
			floor_info.forEach((floor, fi) => { //每层
				let floor_div = document.createElement('div');
				floor_div.className = 'floor';
				floor_div.id = abc + '-' + (fi + 1);
				floor_div.setAttribute('floor_title', (fi + 1) + '层');
				floor.forEach((fns, i) => { //每层南北两侧
					let floor_NS = document.createElement('table');
					floor_NS.className = fns_class[i];
					let room_id = 2 - i + (fi + 1) * 100;
					room_html = i == 0 ? room_cont + room_title : room_title + room_cont;
					fns.forEach((room, ri) => { //每层每侧房间
						if (+room) room = [room, 0];
						let room_td = document.createElement('td');
						room_td.style.width = room[0] * room_w + 'px';
						room_td.className = 'room';
						room_td.classList.add(room_class[room[1]]);
						if (room[1] == 0) {
							room_td.id = abc + '-' + room_id;
							room_id += 2;
							room_td.innerHTML = room_html;
						} else if (room[1] < 6) { //显示公共标识：步梯，电梯，卫生间等
							room_td.innerHTML = room_types[room[1]];
						}
						floor_NS.append(room_td);
					});
					floor_div.append(floor_NS);
				});
				bd_div.append(floor_div);
			});
		}
		return this;
	}
	setInfo(level = 0) {
		//--设置房间信息：修正房间号，部门名称，职务，姓名，房间功能
		let BDS = this;
		let bds = BDS.bdstr.toUpperCase().split('|');
		let room = BDS.dom.querySelectorAll('.work');

		function get_info(info) { //获取房间信息文件内容，解析数据并缓存。
			return Promise.resolve(info).then(info => {
				if (!sessionStorage[info]) {
					return fetch(BDS[info]).then(res => res.text()).then(data => {
						let obj = {};
						data = data.trim().split('\r\n').slice(1).map((n) => n.split(','));
						data.forEach((d, i) => {
							let bd = d[0].split('-')[0];
							let room_id = d[0].split('|');
							if (!!obj[bd] == false) obj[bd] = {};
							room_id.forEach(n => obj[bd][n] = d.slice(1).map(n => n.split(
								'|')).flat());
						});
						sessionStorage[info] = JSON.stringify(obj);
						return sessionStorage[info];
					});
				} else {
					return sessionStorage[info];
				}
			});
		} //==get_info=====
		let BDS_info = get_info('base_info');
		BDS_info.then((data) => { //修正房间号
			data = JSON.parse(data);
			room.forEach(rm => {
				let roomid = rm.id;
				let bd = roomid.split('-')[0];
				let _info = data[bd][roomid];
				rm.id = _info[0];
				let title_div = rm.querySelector('.room-title');
				let title = _info.map(n => n.split('-')[1]);
				if (title.length > 1) {
					title = title.map(n => '<span>' + n + '</span>').join('');
					title_div.classList.add('room_num');
				}
				title_div.innerHTML = title || ' ';
			});
			//---设置房间部门，职务，姓名
			if (level > 0) BDS_info.then(() => {
				get_info('room_info').then(data => {
					data = JSON.parse(data);
					room = BDS.dom.querySelectorAll('.work');
					room.forEach(rm => {
						let roomid = rm.id.split('|')[0];
						let bd = roomid.split('-')[0];
						let rm_cont = rm.querySelector('.room-cont');
						let cont = data[bd][roomid].slice(0, level > 3 ? 1 : level)
							.join('<br>');
						rm_cont.innerHTML = cont;
						if (cont.includes('闲置')) {
							rm.classList.add('unused');
							rm_cont.innerHTML = '';
						}
					});
				});
			}); //---level:1
			//---设置房间功能：资料室，评估办等特殊需求
			if (level == 4) BDS_info.then(() => {
				get_info('room_pg').then(data => {
					data = JSON.parse(data);
					room.forEach(rm => {
						let rmid = rm.id.split('|')[0];
						let bd = rmid.split('-')[0];
						if (rmid in data[bd]) {
							rm.classList.add('room-pg');
							rm.querySelector('.room-cont').innerHTML +=
								`<br>${data[bd][rmid][1]}`;
						}
					});
				});
			}); //---level:4
		});
	} //getInfo===
} //class===
